package cn.com.xy.sms.sdk.ui.popu.part;

import java.util.Map;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TableRow;
import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.util.ChannelContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.SmartSmsSdkUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ThemeUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ViewUtil;
import cn.com.xy.sms.sdk.ui.popu.widget.DuoquHorizItemTable;
import cn.com.xy.sms.sdk.ui.popu.widget.DuoquTableViewShowMoreInfoSec;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.util.ParseManager;

public class BubbleHorizTableBody extends UIPart {

    private static int DEFAULT_SHOW_ROW = 6;
    private static int DEFAULT_LIMIT_ROW = 8;
    private DuoquHorizItemTable mContentListView = null;
    public static final String HINT_TV_FLAG = "hint";
    public static final String HAS_BOTTOM = "has_bottom";

    private static final String TABLE_KEY = "duoqu_table_data_horiz";
    /* SMARTISAN-253 zhaoxiachao 20161128 start */
    private static final String FLIGHT_FLAG = "flight_data_arr";
    private static final String TRAIN_FLAG = "card_arr";
    /* SMARTISAN-253 zhaoxiachao 20161128 end */
    private int mSize = 0;
    
    public BubbleHorizTableBody(Activity mContext, BusinessSmsMessage message, XyCallBack callback, int layoutId,
            ViewGroup root, int partId) {
        super(mContext, message, callback, layoutId, root, partId);
    }

    @Override
    public void initUi() {
        mContentListView = (DuoquHorizItemTable) mView.findViewById(R.id.duoqu_horiz_list);
    }

    @Override
    public void setContent(BusinessSmsMessage message, boolean isRebind) throws Exception {
        this.mMessage = message;
        if (message == null || mContentListView == null) {
            return;
        }
        ThemeUtil.setViewBg(mContext, mView, "", R.drawable.duoqu_v_by_bg_1);

        final String fixKey = (String) message.getValue("m_by_text_d_1");
        int ad = 0;
        if (!StringUtils.isNull(fixKey)) {
            ad++;
        }
        final String origKey = (String) message.getValue("m_by_original");
        if (!StringUtils.isNull(origKey)) {
            ad++;
        }
        /* SMARTISAN-265 zhaoxiachao 20161201 start */
        TableRow tableRow = (TableRow) mView.findViewById(R.id.duoqu_horiz_train_seat);
        final String seatStr = message.getValue(ChannelContentUtil.SEAT_INFO)+"";
        if(!StringUtils.isNull(seatStr)){
            ad++;
        }
        /* SMARTISAN-265 zhaoxiachao 20161201 end */
        int showRows = DEFAULT_SHOW_ROW;
        int maxRows = DEFAULT_LIMIT_ROW;
        try {
            showRows = Integer.parseInt(String.valueOf(message.getValue("default_num_of_items")));
            maxRows = Integer.parseInt(String.valueOf(message.getValue("maximum_num_of_items")));
        } catch (Throwable e) {
            showRows = DEFAULT_SHOW_ROW;
            maxRows = DEFAULT_LIMIT_ROW;
        }
        showRows = showRows - ad;
        maxRows = maxRows - ad;

        final int moreShowRows = showRows;
        
        /* SMARTISAN-313 zhaoxiachao 20161206 start */
        mSize = message.getTableDataSize(TABLE_KEY);
        if(mSize >= 1){
            mView.setVisibility(View.VISIBLE);
        }else{
            if(StringUtils.isNull(seatStr)){
                mView.setVisibility(View.GONE);
                return;
            }else{
                mView.setVisibility(View.VISIBLE);
                mSize = 1;
            }
        }
        /* SMARTISAN-313 zhaoxiachao 20161206 end */
        
        mContentListView.setDefaultShowRow(showRows==0?mSize:showRows);
        mContentListView.setmDefaultLimitDataSize(maxRows);
        
        if(StringUtils.isNull(message.getValue(HAS_BOTTOM)+"")){
            message.putValue(HAS_BOTTOM, ChannelContentUtil.hasBottomBotton(message));
        }

        setLayoutMargin(message, mSize, showRows, origKey, fixKey);
        
        mContentListView.setContentList(message, mSize, TABLE_KEY, isRebind);
        /* SMARTISAN-265 zhaoxiachao 20161201 start */
        mContentListView.setTrainSeatInfo(message, tableRow, seatStr, isRebind);
        if(StringUtils.isNull(seatStr)){
            mContentListView.getChildAt(0).setVisibility(View.GONE);
        }else{
            mContentListView.getChildAt(0).setVisibility(View.VISIBLE);
        }
        /* SMARTISAN-265 zhaoxiachao 20161201 end */
        mContentListView.mDuoquTableViewShowMoreInfo.setMoreClickCall(new XyCallBack() {
            
            @Override
            public void execute(Object... obj) {
                // TODO Auto-generated method stub
                setLayoutMargin(mMessage, mSize, moreShowRows, origKey, fixKey);
            }
        });
    }

    public void setLayoutMargin(BusinessSmsMessage message, int size, int showRows, String origKey, String fixKey){
        /* SMARTISAN-253 zhaoxiachao 20161128 start */
        String obAir = mMessage.getValue(FLIGHT_FLAG)+"";
        String obTrain = mMessage.getValue(TRAIN_FLAG)+"";
        String flightFlag = !StringUtils.isNull(obAir) && !"[]".equalsIgnoreCase(obAir) ? "true" : "";
        String trainFlag = !StringUtils.isNull(obTrain) && !"[]".equalsIgnoreCase(obTrain) ? "true" : "";
        /* SMARTISAN-253 zhaoxiachao 20161128 end */

        int marBottom = 0;
        int marTop = 0;
        // 判断当前是否是最后一个
        if(mSize < 2){
            marBottom = ViewUtil.dp2px(mContext, 5);
        } else if(size > showRows && !mContentListView.mDuoquTableViewShowMoreInfo.getExpanded(message)){  // 如果有更多按钮
            marBottom = ViewUtil.dp2px(mContext, 0);
        } else if(size > showRows && mContentListView.mDuoquTableViewShowMoreInfo.getExpanded(message)){   // 如果更多按钮收起了
            marBottom = ViewUtil.dp2px(mContext, 5);
        }else if((!StringUtils.isNull(flightFlag) || !StringUtils.isNull(trainFlag))){
            marBottom = ViewUtil.dp2px(mContext, 5);
        } else if (!StringUtils.isNull(origKey)) {   // 短信的提示内容
            marBottom = ViewUtil.dp2px(mContext, 2);
        } else if ((Boolean) message.getValue(BubbleHorizTableBody.HAS_BOTTOM)) {  // 是否具有底部按钮
            marBottom = ViewUtil.dp2px(mContext, 5);
        } else {
            /* SMARTISAN-464 zhaoxiachao 20170307 start */
            marBottom = ViewUtil.dp2px(mContext, -3);
            /* SMARTISAN-464 zhaoxiachao 20170307 end */
        }
        
        if(!StringUtils.isNull(flightFlag) || !StringUtils.isNull(trainFlag)){
            marTop = ViewUtil.dp2px(mContext, 0);
        }else if(StringUtils.isNull(fixKey)){
            marTop = ViewUtil.dp2px(mContext, 15);
        } else{
            marTop = ViewUtil.dp2px(mContext, 11);
        }
        
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.bottomMargin = marBottom;
        params.topMargin = marTop;
        mContentListView.setLayoutParams(params);
    }

    @Override
    public void destroy() {
        /* SMARTISAN-299 zhaoxiachao 20161208 start */
//        super.destroy();
        mCallback = null;
        try{
            mMessage.bubbleJsonObj.put(DuoquTableViewShowMoreInfoSec.KEY_EXPANDED, false);
            ParseManager.updateMatchCacheManager(mMessage);
        }catch(Throwable ex){}
        /* SMARTISAN-299 zhaoxiachao 20161208 end */
    }

    @Override
    public void changeData(Map<String, Object> param) {
        try {
            this.setContent(mMessage, true);
        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog("BubbleVerticalTableBody changeData error:", e);
        }
    }
}
